package com.group.nepm.statisticDataManagement.controller;

import com.group.common.utils.MyUtils;
import com.group.nepg.entity.AccurateAqiLevel;
import com.group.nepm.statisticDataManagement.entity.StatisticData;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class AqiDistributionStatisticController {
    @FXML
    private Label level1;
    @FXML
    private Label level2;
    @FXML
    private Label level3;
    @FXML
    private Label level4;
    @FXML
    private Label level5;
    @FXML
    private Label level6;

    public void adminOut(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/common/view/NEPMLoginView.fxml");
    }
    public void toPublicSuperviseDataList(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/publicSuperviseDataManagement/view/PublicSuperviseDataList.fxml");
    }
    public void toConfirmAqiDataList(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/publicSuperviseDataManagement/view/ConfirmAqiDataList.fxml");
    }
    public void toProvinceStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/provinceStatistic.fxml");
    }
    public void toAqiDistributionStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/aqiDistributionStatistic.fxml");
    }
    public void toAqiTendencyStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/aqiTendencyStatistic.fxml");
    }
    public void toOtherDataStatistic(ActionEvent event) {
        MyUtils.navigateToView(event, "group/nepm/statisticDataManagement/view/otherDataStatistic.fxml");
    }

    public void initialize() {
        String filePath = "group/data/statistic_data_arraylist.dat";
        ArrayList<StatisticData> dataList = MyUtils.readFileToArrayList(filePath);
        Map<AccurateAqiLevel, Integer> levelCountMap = new HashMap<>();

        for (AccurateAqiLevel level : AccurateAqiLevel.values()) {
            levelCountMap.put(level, 0);
        }

        if (dataList != null) {
            for (StatisticData data : dataList) {
                AccurateAqiLevel level = data.getAccurateAqiData().getAccurateAqiLevel();
                if (level != null && levelCountMap.containsKey(level)) {
                    levelCountMap.put(level, levelCountMap.get(level) + 1);
                }
            }
        }

        level1.setText(String.valueOf(levelCountMap.get(AccurateAqiLevel.ACCURATE_AQI_LEVEL_1)));
        level2.setText(String.valueOf(levelCountMap.get(AccurateAqiLevel.ACCURATE_AQI_LEVEL_2)));
        level3.setText(String.valueOf(levelCountMap.get(AccurateAqiLevel.ACCURATE_AQI_LEVEL_3)));
        level4.setText(String.valueOf(levelCountMap.get(AccurateAqiLevel.ACCURATE_AQI_LEVEL_4)));
        level5.setText(String.valueOf(levelCountMap.get(AccurateAqiLevel.ACCURATE_AQI_LEVEL_5)));
        level6.setText(String.valueOf(levelCountMap.get(AccurateAqiLevel.ACCURATE_AQI_LEVEL_6)));
    }
}
